package com.lagou.server;

import com.lagou.model.User;

import java.io.*;
import java.util.List;
import java.util.logging.ConsoleHandler;

/**
 * 编写从文件读取所有学生信息以及写入所有学生信息的方法
 */
public class ServerDatabase {
    /**
     * 存储list到文件
     * @param path
     * @param list
     */
    public static void FileWriteList(String path,List list){
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(path);
            ObjectOutputStream stream = new ObjectOutputStream(outputStream);
            stream.writeObject(list);
            stream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 从文件读取list
     * @param path
     */
    public static List FileInputList(String path){
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path);
            ObjectInputStream stream = new ObjectInputStream(inputStream);
            List list = (List) stream.readObject();
            inputStream.close();
            stream.close();
            return list;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

/*
    public static void duwenjian(){
        ObjectInputStream ois = null;
        try {
            // 1.创建ObjectInputStream类型的对象与d:/a.txt文件关联
            ois = new ObjectInputStream(new FileInputStream("./Student.txt"));
            // 2.从输入流中读取一个对象并打印
            Object obj = ois.readObject();
            System.out.println("读取到的对象是：" + obj); // qidian 123456 13511258688  null
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 3.关闭流对象并释放有关的资源
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void xiewenjian() {
        ObjectOutputStream oos = null;
        try {
            // 1.创建ObjectOutputStream类型的对象与d:/a.txt文件关联
            oos = new ObjectOutputStream(new FileOutputStream("d:/a.txt"));
            // 2.准备一个User类型的对象并初始化
            User user = new User("qidian", "123456");
            // 3.将整个User类型的对象写入输出流
            oos.writeObject(user);
            System.out.println("写入对象成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.关闭流对象并释放有关的资源
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
*/


}
